package com.lagou.controller;

import com.lagou.domain.Course;
import com.lagou.domain.CourseVO;
import com.lagou.domain.ResponseResult;
import com.lagou.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 李树飞
 */
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 多条件查询
     * @param courseVO
     * @return
     */
    @RequestMapping("/findCourseByConditioin")
    public ResponseResult findCourseByConditioin(@RequestBody CourseVO courseVO) {

        List<Course> list = courseService.findCourseByCondition(courseVO);

        return new ResponseResult(true, 200, "响应成功", list);
    }


    /**
     * 图片上传和回显
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @RequestMapping("/courseUpload")
    public ResponseResult courseUpload(@RequestParam("file")MultipartFile file, HttpServletRequest request) throws IOException {
        //判断文件是否为控
        if (file.isEmpty()) {
            throw new RuntimeException();
        }

        ////获取文件真实路径
        String realPath = request.getServletContext().getRealPath("/");

        //截取文件路径
        String newRealPath = realPath.substring(0, realPath.indexOf("ssm_web"));

        //获取文件名
        String fileName = file.getOriginalFilename();

        //拼接新的文件名
        String newFileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."));

        //创建新的文件并上传
        String upLoadPath = newRealPath + "upload\\";
        File newFile = new File(upLoadPath, newFileName);

        //判断目录是否存在
        if (!newFile.getParentFile().exists()) {
            newFile.getParentFile().mkdirs();
            System.out.println("创建目录" + newFile);
        }
        file.transferTo(newFile);

        Map<String, String> map = new HashMap<>();
        map.put("fileName", newFileName);
        map.put("filePath", "http://localhost:8080/upload/" + newFileName);

        return new ResponseResult(true, 200, "上传成功", map);
    }

    /**
     * 添加和修改课程
     * @param courseVO
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @RequestMapping("/saveOrUpdateCourse")
    public ResponseResult saveOrUpdateCourse(@RequestBody CourseVO courseVO) throws InvocationTargetException, IllegalAccessException {

        //判断CourseId是否为空
        if (courseVO.getId() == null) {
            courseService.saveCourseOrTeacher(courseVO);
            return new ResponseResult(true, 200, "添加成功", null);
        } else {
            courseService.updateCourseOrTeacher(courseVO);
            return new ResponseResult(true, 200, "更新成功", null);
        }
    }

    /**
     * 回显课程信息（根据id查询对应的信息及讲师信息）
     * @param id
     * @return
     */
    @RequestMapping("/findCourseById")
    public ResponseResult findCourseById(Integer id) {

        CourseVO courseVO = courseService.findCourseById(id);

        return new ResponseResult(true,200,"响应成功",courseVO);
    }

    /**
     * 修改课程状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("/updateCourseStatus")
    public ResponseResult updateCourseStatus(@RequestParam Integer id, @RequestParam Integer status) {
        courseService.updateCourseStatus(id, status);

        Map<String, Integer> map = new HashMap<>();
        map.put("status", status);
        return new ResponseResult(true,200,"课程状态变更成功",map);
    }

}
